// pages/shangluoweixinbushu/shangluoweixinbushu.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zongshuju: '',
    POINTS: '',
    flag: true, //设置节流
    HISTORY_POINTS:'',
    WALKING_ID:'',
  },
  xiangqin() {
    wx.navigateTo({
      url: '../../pages/shangluoweixinbushuminxi/shangluoweixinbushuminxi',

    })
  },
  duihuan() {
    wx.navigateTo({
      url: '../../pages/shangluoweixinbushujifenduihuan/shangluoweixinbushujifenduihuan?data=' + JSON.stringify(this.data.POINTS)

    })
  },


  back() {
    // wx.switchTab({
    //   url: '../../pages/newshouye/newshouye',
    // })
    wx.navigateBack({
      delta: 2
    })
  },

  qidai() {
    wx.navigateTo({
      url: '../../pages/shangluoweixinbushuchoujiang/shangluoweixinbushuchoujiang?data=' + JSON.stringify(this.data.WALKING_ID)

    })
  },





  huoqu() {

    if (this.data.flag) {

      this.data.flag = false



      this.setData({
        openid: app.globalData.openid,
        jiami: app.globalData.jiami,
        OPEN_ID: app.globalData.openid,
        session_key: app.globalData.session_key,
      })

      let that = this

      wx.getWeRunData({

        
        success(res) {

        

          // 拿 encryptedData 到开发者后台解密开放数据
          const encryptedData = res.encryptedData
          console.log(encryptedData);
          console.log(res.iv);
    
          wx.request({
            url: app.globalData.interface+'/shangluoTicket/API_5903_WalkingNumberSignIn',
            method: 'post',
            data: {
              WALKING_ID: that.data.WALKING_ID,
              iv: res.iv,
              sessionKey: that.data.session_key,
              encryptedData: encryptedData,

              OPEN_UUID: app.globalData.jiami,
              OPEN_ID: app.globalData.openid,
              session_key: app.globalData.session_key,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
              console.log(res);
              wx.showLoading({
                title: "正在签到",
                mask: true
              })

              if (res.data.result == 'error') {
                wx.showToast({
                  title: '签到成功',
                  icon: 'error',
                  duration: 1500,
                })

                wx.showModal({
                  title: '温馨提示',
                  content: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后

                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                    }
                  }
                })
              } else {
                wx.showToast({
                  title: '签到成功',
                  icon: 'success',
                  duration: 1500,
                })
                wx.showModal({
                  title: '温馨提示',
                  content: res.data.data,
                  success: function (res) {
                    if (res.confirm) { //这里是点击了确定以后
                      that.shuju()
               
                    } else { //这里是点击了取消以后
                      console.log('用户点击取消')
                      that.shuju()
          
                    }
                  }
                })
              }

            }
          })
        }


      })







      setTimeout(() => {
        this.data.flag = true
      }, 6000)
    }


  },



  shuju() {
    this.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

    wx.request({
      url: app.globalData.interface+'/shangluoTicket/API_5907_WalkingUserSelect',
      method: 'post',
      data: {


        OPEN_UUID: app.globalData.jiami,
        OPEN_ID: app.globalData.openid,
        session_key: app.globalData.session_key,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          POINTS: res.data.data.POINTS,
          zongshuju: res.data.data,
          HISTORY_POINTS: res.data.data.HISTORY_POINTS,
          WALKING_ID: res.data.data.WALKING_ID
        })


      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    setTimeout(() => {
      this.shuju()
    }, 500)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})